package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.entity.SecurityStaffCertificateRecord;
import com.ruoyi.system.entity.SysRemind;
import com.ruoyi.system.mapper.SecurityStaffCertificateRecordMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.SecurityStaffCertificateRecordService;
import com.ruoyi.system.service.SysRemindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 安全_企业基本信息_员工证书档案 服务实现类
 * </p>
 *
 * @since 2023-03-15
 */
@Service("securityStaffCertificateRecordService")
public class SecurityStaffCertificateRecordServiceImpl implements SecurityStaffCertificateRecordService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysRemindService sysRemindService;
    @Resource
    private SecurityStaffCertificateRecordMapper staffCertificateRecordMapper;
    @Resource
    private EnterpriseWXServiceImpl enterpriseWXService;

    @Override
    public List<SecurityStaffCertificateRecord> queryByPage(SecurityStaffCertificateRecord staffCertificateRecord) {
        return staffCertificateRecordMapper.queryAllByLimit(staffCertificateRecord);
    }

    @Override
    public SecurityStaffCertificateRecord queryById(Integer id) {
        return staffCertificateRecordMapper.queryById(id);
    }

    @Override
    public SecurityStaffCertificateRecord insert(SecurityStaffCertificateRecord staffCertificateRecord) {
        //staffCertificateRecord.setCreateUser(SecurityUtils.getUsername());
        this.staffCertificateRecordMapper.insert(staffCertificateRecord);
        return staffCertificateRecord;
    }

    @Override
    public SecurityStaffCertificateRecord update(SecurityStaffCertificateRecord staffCertificateRecord) {
        this.staffCertificateRecordMapper.update(staffCertificateRecord);
        return this.queryById(staffCertificateRecord.getId());
    }

    @Override
    public boolean deleteById(Integer id) {
        return this.staffCertificateRecordMapper.deleteById(id) > 0;
    }

    @Override
    public String importExcel(List<SecurityStaffCertificateRecord> securityStaffCertificateRecords, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(securityStaffCertificateRecords) || securityStaffCertificateRecords.size() == 0) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        securityStaffCertificateRecords.forEach(val -> {
            staffCertificateRecordMapper.insert(val);
        });
        return "成功";
    }

    @Scheduled(cron = "0 0 8 * * ?")
    public void checkCertificatePeriod() {
        SecurityStaffCertificateRecord securityStaffCertificateRecord = new SecurityStaffCertificateRecord();
        List<SecurityStaffCertificateRecord> securityStaffCertificateRecords =
                staffCertificateRecordMapper.queryAllByLimit(securityStaffCertificateRecord);

        if (CollectionUtils.isEmpty(securityStaffCertificateRecords)) {
            return;
        }

        Date date = new Date();
        for (SecurityStaffCertificateRecord var : securityStaffCertificateRecords) {
            String recordReminderPeriod = var.getRecordReminderPeriod();
            if (StringUtils.isEmpty(recordReminderPeriod)) {
                continue;
            }

            boolean after = date.after(DateUtils.parseDate(recordReminderPeriod));
            if (after) {
                String stringBuffer = "姓名:" + var.getRecordName() + "证书编号:" + var.getRecordCertificateNumber() + "即将到期。";

                SysUser sysUser = new SysUser();
                sysUser.setNickName(var.getRecordName());
                List<SysUser> sysUsers = sysUserMapper.selectWxIdByNickName(sysUser);
                if (CollectionUtils.isEmpty(sysUsers)) {
                    continue;
                }

                SysUser user = sysUsers.stream().findFirst().orElse(new SysUser());

                sysRemindService.insert(SysRemind.TYPE_PROMPT, stringBuffer, user.getUserId().intValue());

                EnterpriseWXServiceImpl.SendMessageRequest sendMessageRequest = new EnterpriseWXServiceImpl.SendMessageRequest();
                sendMessageRequest.setTouser(user.getWxUserId());
                sendMessageRequest.setMsgtype("text");
                sendMessageRequest.setAgentid(1000319);
                EnterpriseWXServiceImpl.MessageContent messageContent = new EnterpriseWXServiceImpl.MessageContent();
                messageContent.setContent(stringBuffer);
                sendMessageRequest.setText(messageContent);
                enterpriseWXService.pushMessage(sendMessageRequest);
            }
        }
    }

}
